﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Reflection;
using System.Resources;
using System.Globalization;
using System.Threading;

namespace ALite.WP7.Core
{
    /// <summary>
    /// 多语言支持基类
    /// </summary>
    public abstract class Resources : INotifyPropertyChanged
    {
        private ResourceManager _Resx;

        private CultureInfo _Culture;
        /// <summary>
        /// 当前的语言
        /// </summary>
        public CultureInfo Culture
        {
            get { return _Culture; }
            set
            {
                _Culture = value;

                // 更新所有的绑定显示
                this.UpdateBindings();
            }
        }

        public Resources()
        {
            // 设置初始的语言类型与系统语言相通
            this.Culture = Thread.CurrentThread.CurrentCulture;

            // 构建ResourceManager实例
            _Resx = this.Initialize();
        }

        public Resources(CultureInfo culture)
        {
            Culture = culture;

            // 构建ResourceManager实例
            _Resx = this.Initialize();
        }

        protected abstract ResourceManager Initialize();

        /// <summary>
        /// 索引值方式访问多语言字符集
        /// </summary>
        /// <param name="resourceName"></param>
        /// <returns></returns>
        public string this[string resourceName]
        {
            get
            {
                return _Resx.GetString(resourceName, this.Culture);
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        // 通知更新所有绑定的数据显示
        public void UpdateBindings()
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("Item[]"));
            }
        }
    }
}
